package com.qf.service.impl;

import com.qf.dfa.SensitiveWordFilter;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainService;
import com.qf.util.PushMsgUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 *
 * 敏感词策略
 * @author YDW
 * projectName:project2202_4
 * time:2022/6/20 22:30
 * description:
 */
@Service("dirtyWords")
@Slf4j
public class DirtyWordsFilterServiceImpl implements FilterChainService {
    @Autowired
    PushMsgUtil pushMsgUtil;
    @Override
    public boolean strategy(StandardSubmit submit) {
        /*
        * 提供短信息安全过滤功能，对非法信息和反动信息进行过滤。
        * 可以先用IK分词对短信发送内容进行分词，然后用分词后的内容依次查询缓存中的敏感词
        * 如果短信发送内容存在敏感词就把发送日志存到Elasticsearch
        * 如果同时是HTTP的发送方式就模拟出状态报告推送给客户
        *
        * */




        log.info("[策略模块 -- 敏感词策略] 策略开始执行！ submit={}",submit);
        //获取消息内容
        String messageContent = submit.getMessageContent();
        SensitiveWordFilter instance = SensitiveWordFilter.getInstance();
        int i = instance.checkSensitiveWord(messageContent,0,2);
        if(i!=0){
            log.info("[策略模块 -- 敏感词策略] 存在脏字！");
            String s = instance.replaceSensitiveWord(messageContent, 0, "*");
            Set<String> sensitiveWord = instance.getSensitiveWord(messageContent, 2);
            pushMsgUtil.sendLog(submit,"包含敏感词: {}"+sensitiveWord);

            //替换脏字，存入submit
            submit.setMessageContent(s);
            log.info("[策略模块 -- 敏感词策略] 替换脏字完成！ submit={}",submit);


        }

        log.info("[策略模块 -- 敏感词策略] 执行完成！ submit={}",submit);

        return true;
    }
}
